<?php
/**
 * 分类
 * @date    2020-01-01
 * @author  kiro
 * @email   294843009@qq.com
 * @version 1.0
 */

namespace App\Http\Controllers\Api\V1\Admin;

use App\Models\BookType;
use App\Models\New_Category;
use Illuminate\Http\Request;

class BookTypeController extends BaseController
{
    public function index(BookType $type, Request $request)
    {
        $model = $type->newQuery();
        //获取单本目录
        if ($request->has('model_id') && !empty($request->model_id)) {
            $model->where('model_id', $request->model_id);
        }
        if ($request->has('pid')) {
            $model->where('pid', $request->input('pid', 0));
        } else {
            $model->where('pid', $request->input('pid', 0));
        }
        $model->orderBy('sort', 'ASC');
        if (empty($request->input())) {
            $list = [];
        } else {
            $list = $model->where('status', 1)->paginate($this->page_size);
        }
        foreach ($list as &$value) {
            $children = BookType::where('pid', $value['id'])->get();
            if ($children) {
                $value['children'] = $children;
            }
        }

//        $status = request()->input('status');
//        $list = New_Category::where(function ($query) use ($status) {
//                ($status !== null) AND $query->where('status', $status);
//            })->orderBy('sort', 'ASC')
//            ->paginate($this->page_size);

        return $this->success('成功', $list);
    }

    public function getParentCategoryOptions(BookType $type, Request $request)
    {
        $model = $type->newQuery();
        //获取单本目录
        if ($request->has('model_id') && !empty($request->model_id)) {
            $model->where('model_id', $request->model_id);
        }
        if ($request->has('pid')) {
            $model->where('pid', $request->input('pid', 0));
        } else {
            $model->where('pid', $request->input('pid', 0));
        }
        $model->orderBy('sort', 'ASC');
        if (empty($request->input())) {
            $list = [];
        } else {
            $list = $model->where('status', 1)->get();
        }
        foreach ($list as &$value) {
            $children = BookType::where('pid', $value['id'])->get();
            if ($children) {
                $value['children'] = $children;
            }
        }
        return $this->success('成功', $list);
    }

    public function getCategoryOptions(BookType $type, Request $request)
    {
        $model = $type->newQuery();
        //获取单本目录
        if ($request->has('model_id') && !empty($request->model_id)) {
            $model->where('model_id', $request->model_id);
        }
        if ($request->has('pid')) {
            $model->where('pid', $request->input('pid', 0));
        } else {
            $model->where('pid', $request->input('pid', 0));
        }
        $model->orderBy('sort', 'ASC');
        if (empty($request->input())) {
            $list = [];
        } else {
            $list = $model->where('status', 1)->get();
        }
        foreach ($list as &$value) {
            $children = BookType::where('pid', $value['id'])->get();
            if ($children) {
                $value['children'] = $children;
            }
        }
//        $list = New_Category::where('status', 1)->get();

        return $this->success('成功', $list);
    }

    public function store(Request $request)
    {
        if (empty($request->name)) {
            return $this->error('分类名称不能为空');
        }
        $model = new BookType();
        $model->name = $request->name;
        $model->ico = $request->icon;
        $model->sort = $request->sort;
        $model->pid = is_null($request->input('pid', 0)) ? 0 : $request->input('pid', 0);
        $model->model_id = $request->model_id;
        $model->status = 1;
        $result = $model->save();
        if ($result) {
            return $this->success('添加成功');
        } else {
            return $this->error('添加失败');
        }
    }

    public function update(Request $request, $booktype)
    {
        if (empty($request->name)) {
            return $this->error('分类名称不能为空');
        }
        $model = BookType::find($booktype);
        $request->name AND $model->name = $request->name;
        $request->sort AND $model->sort = $request->sort;
        $request->ico AND $model->icon = $request->icon;
        $request->pid AND $model->pid = $request->pid;
        $request->model_id AND $model->model_id = $request->model_id;
        $result = $model->save();
        if ($result) {
            return $this->success('修改成功', $result);
        } else {
            return $this->error('修改失败');
        }
    }

    public function destroy($booktype)
    {
        $result = BookType::where('id', $booktype)->update(['status' => 0]);
        if ($result) {
            return $this->success('禁用成功', $result);
        } else {
            return $this->error('禁用失败');
        }
    }

    public function batchDisable(Request $request)
    {
        $input = $request->toArray();
        if (!in_array($input['status'], [0, 1])) {
            return $this->error('状态不正确');
        }
        $result = New_Category::whereIn('id', $input['selection'])->update(['status' => $input['status']]);
        if ($result) {
            return $this->success('保存成功');
        } else {
            return $this->error('保存失败');
        }
    }
}
